package com.jd.bluej.dubboj.service;
import java.util.Random;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.jd.bluej.dubboj.modle.ResponseResult;
import com.jd.bluej.dubboj.modle.Sku;
import com.jd.bluej.dubboj.modle.User;


/**
 * Created by bluej on 2016/11/19.
 */


@Path("/user")
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Produces({ ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8 })
@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private ItemService itemService;
	
    //http://localhost:9090/service/user/1
    //占位符方式restful风格
    @GET
    @Path("/{id}")
    public ResponseResult<User> getUserById(@PathParam("id") String id) {
        System.out.println("------------getUserById---------------"+"id:"+id);
        ResponseResult<User> responseResult =null;

        User user = createUser();
        responseResult = new ResponseResult<User>(user);
        
        return responseResult;
    }

    //http://localhost:9090/service/user/getUserByName?name=lihua
    //地址栏追加参数方式
    @GET
    @Path("getUserByName")
    public ResponseResult<User> getUserByName(@QueryParam("name") String name){
        System.out.println("------------getUserByName---------------"+"name:"+name);
        ResponseResult<User> responseResult =null;
        
        User user = createUser();
        responseResult = new ResponseResult<User>(user);
        
        return responseResult;
    }
    
    @GET
    @Path("getSku/{id}")
    public ResponseResult<Sku> getSku(@PathParam("id") String id){
    	ResponseResult<Sku> responseResult =null;
    	System.out.println("id:"+id);
    	Sku sku = itemService.getSku();
    	responseResult = new ResponseResult<Sku>(sku);
    	
    	return responseResult;
    }

    //创建user对象
    private User createUser(){
        User user =new User();
        user.setId(new Random().nextInt(999999));
        user.setName("战三");
        user.setAge(34);
        user.setSex("男");
        return  user;
    }
}